package com.fubon.wloan.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * HTTP请求
 * 
 * @author TANGYINGQUAN360
 * 
 */
public class HttpUtils {
	public static String sendJsonWithHttp(String surl, String json) throws Exception {
		URL url = new URL(surl);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
		conn.setRequestMethod("POST");// 提交模式
		conn.setRequestProperty("Content-Length", json.getBytes().length + "");
		conn.setConnectTimeout(30000);// 连接超时单位毫秒 //
		conn.setReadTimeout(50000);// 读取超时 单位毫秒
		conn.setDoOutput(true);// 是否输入参数
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.connect();
		DataOutputStream out = new DataOutputStream(conn.getOutputStream());
		out.write(json.getBytes());
		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		StringBuffer sb = new StringBuffer();
		String line;
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		reader.close();
		conn.disconnect();
		return sb.toString();
	}

}
